//
//  BVSearchView.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/20.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVSearchView.h"
#import "BVHotSearchCell.h"
#import "UIView+Extension.h"
#import "BVSearch.h"

#define HOT_SEARCH_CELL @"BVHotSearchCell"
#define HISTORY_SEARCH_CELL @"HistorySearchCell"
#define CGRECT CGRectMake(0, 0, ScreenW, ScreenH - 64)
#define HEIGHT_FOR_CELL 40
#define LINE_LABEL_HEIGHT 0.5

@interface BVSearchView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) BVSearch *search;

@end

@implementation BVSearchView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

        [self setupTableView];
    }
    return self;
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRECT style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:self.tableView];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 1 : self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BVHotSearchCell *hotCell;
    UITableViewCell *historyCell;
    if (indexPath.section == 0) {
        hotCell = [tableView dequeueReusableCellWithIdentifier:HOT_SEARCH_CELL];
        if (!hotCell) {
            hotCell = [[BVHotSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HOT_SEARCH_CELL];
            [hotCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        hotCell.hotBlock = ^(NSInteger tag, NSString *ID){
            [self.search saveSearchText:ID];
            [self reloadData];
        };
        return hotCell;
    } else {
        historyCell = [tableView dequeueReusableCellWithIdentifier:HISTORY_SEARCH_CELL];
        if (!historyCell) {
            historyCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HISTORY_SEARCH_CELL];
            [historyCell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenW, LINE_LABEL_HEIGHT)];
            [lineLabel setBackgroundColor:[UIColor BVLineGrayColor]];
            [historyCell.contentView addSubview:lineLabel];
        }
        historyCell.textLabel.text = self.dataSource[indexPath.row];
        historyCell.textLabel.textColor = [UIColor BVBaseTextBlackColor];
        historyCell.textLabel.font = [UIFont systemFontOfSize:15];
        return historyCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 140 : HEIGHT_FOR_CELL;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, HEIGHT_FOR_CELL)];
    sectionView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 70, sectionView.height)];
    [headerLabel setText:section == 0 ? @"热门搜索" : @"历史搜索"];
    [headerLabel setFont:[UIFont systemFontOfSize:16]];
    [headerLabel setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    [headerLabel setTextColor:[UIColor BVBaseTextBlackColor]];
    [sectionView addSubview:headerLabel];
    return sectionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return HEIGHT_FOR_CELL;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == 1 ? HEIGHT_FOR_CELL : 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, HEIGHT_FOR_CELL)];
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenW, LINE_LABEL_HEIGHT)];
    [lineLabel setBackgroundColor:[UIColor BVLineGrayColor]];
    [footerView addSubview:lineLabel];

    if (section == 1) {
        UIButton *clean = [[UIButton alloc] init];
        [clean setFrame:CGRectMake(0, LINE_LABEL_HEIGHT, ScreenW, HEIGHT_FOR_CELL)];
        [clean setUserInteractionEnabled:self.dataSource.count != 0 ? YES : NO];
        [clean.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [clean setBackgroundColor:[UIColor whiteColor]];
        [clean setTitle:self.dataSource.count != 0 ? @"清空历史记录" : @"无搜索记录" forState:UIControlStateNormal];
        [clean setTitleColor:[UIColor BVButtonGrayColor] forState:UIControlStateNormal];
        [clean addTarget:self action:@selector(cleanHistorySearch) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:clean];
        return footerView;
    } else {
        return [[UIView alloc] init];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSLog(@"%ld",indexPath.row);
    }
}

#pragma mark -
#pragma mark - 刷新数据
- (void)reloadData {
    /*刷新数据源*/
    self.dataSource = [self.search fetchSearchText];
    [self.tableView reloadData];
}

#pragma mark -
#pragma mark - 清空历史记录
- (void)cleanHistorySearch {
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

- (BVSearch *)search {
    if (!_search) {
        _search = [[BVSearch alloc] init];
    }
    return _search;
}
@end
